package com.wframe.project.jnd.wx.controller;

import com.wframe.project.jnd.rest.util.ClientException;
import com.wframe.project.jnd.rest.util.JsonReturn;
import com.wframe.project.jnd.rest.util.WebSession;
import com.wframe.project.jnd.services.ProductService;
import com.wframe.project.jnd.wx.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * Created by lwb on 2017/4/22.
 */
@RequestMapping("/we/favorite")
@RestController
public class WechatCollectController extends BaseController {

    @Autowired
    private ProductService productService;

    /**
     * 收藏商品
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public String saveOrUpdateFavorite(@PathVariable("id") String priceId, Integer action) throws Exception {
        productService.favorite(WebSession.getUserId(session), priceId, action);

        return new JsonReturn().toString();
    }

    /**
     * 我的收藏列表
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public String listAllFavorites(@RequestParam(value = "pno", defaultValue = "1") Integer pageNo) throws ClientException {
        String userId = WebSession.getUserId(session);
        JsonReturn json = new JsonReturn();
        json.putData(productService.findFavorites(userId, Constant.DEFAULT_PAGE_SIZE, pageNo, WebSession.checkWbUserAuth(userId)));

        return json.toString();
    }

    /**
     * 删除收藏
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public String deleteFavorites(@RequestParam("ids[]") String[] ids) throws ClientException {
        Assert.notEmpty(ids, "删除收藏方法参数为空");

        productService.deleteFavorites(WebSession.getUserId(session), ids);

        return new JsonReturn().toString();
    }

}